/****************************************************************************/  
/* NAME:           osmolator.c                                              */  
/* PURPOSE:        Osmolator management                                     */  
/* CREATED_BY:     Vyx                                                      */  
/* CREATION_DATE:  07/05/2012                                               */  
/************************************** (C) Copyright 2012 AquaLink *********/ 

#include "osmolator.h"
#include "Hal_InputManagement.h"
#include "Hal_OutputManagement.h"

/* Plug sensor level between Vcc and its gpio */
/* True sensor hight position                 */
/* False sensor low position                  */

/* SENSOR LOW and HIGH
 TRUE  FALSE
        ___
  |    |   |
 _|_   |___|
|   |    |
|___|    |

*/

/* SENSOR RESERVE
 FALSE  TRUE
        ___
  |    |   |
 _|_   |___|
|   |    |
|___|    |

*/

#ifdef _OSMOLATOR

#define Osmo_TASK_DELAY    (5000) /* 5s */ 

/**        
  * @brief  Start the pump output.        
  * @param  none        
  * @retval None      
  */   
#define Osmo_StartPump()   (OUTPUT_SetOutputValue(OSMOLATOR_PUMP, TRUE))

/**        
  * @brief  Stop the pump output.        
  * @param  none        
  * @retval None      
  */   
#define Osmo_StopPump()    (OUTPUT_SetOutputValue(OSMOLATOR_PUMP, FALSE))

/**
  * @brief  Main function of osmolator module.
  * @param  none
  * @retval None
  */
void vTaskCTRL_OSMOLATOR()
{
    portTickType       xLastWakeTime;
    Osmo_Water_level_t Level;

    /* init */
    xLastWakeTime    = xTaskGetTickCount();
    Level           = OSMO_HIGH_LEVEL;

    Osmolator_init();
    
    while(1)
    {
        /* if reserve water is NOT empty */
        if (INPUT_GetInputValue(OSMOLATOR_SENSOR_RESERVE) == TRUE)   
        {
            /* ckeck the water level */
            Level = Osmo_CheckWaterLevel();
            
            /* if manual mode */
            if (INPUT_GetInputValue(OSMOLATOR_MANUAL_START) == TRUE)
            {
                /* protection to start pump only if water level is not high */
                if (Level != OSMO_HIGH_LEVEL)
                {
                    send_message(OSMOLATOR_MODULE_ID, normal, "pump start");
                    Osmo_StartPump();
                }
            }
            /* if auto mode */
            else    
            {
                switch(Level)
                {
                    case OSMO_LOW_LEVEL:
                    {
                        send_message(OSMOLATOR_MODULE_ID, normal, "water level : low");
                
                        /* start pump if is not start */
                        if ((INPUT_GetInputValue(OSMOLATOR_SENSOR_RESERVE) == TRUE) && 
                            (OUTPUT_GetOutputValue(OSMOLATOR_PUMP)         == FALSE))
                        {
                            send_message(OSMOLATOR_MODULE_ID, normal, "pump start");
                            Osmo_StartPump();
                        }
                        break;
                    }
                    case OSMO_MIDDLE_LEVEL:
                    {
                        send_message(OSMOLATOR_MODULE_ID, normal, "water level : middle");
                        break;   
                    }
                    case OSMO_HIGH_LEVEL:
                    default:
                    {
                        send_message(OSMOLATOR_MODULE_ID, normal, "water level : high");
                        
                        /* stop pump if is start */
                        if (OUTPUT_GetOutputValue(OSMOLATOR_PUMP) == TRUE)
                        {
                            send_message(OSMOLATOR_MODULE_ID, normal, "pump stop");
                            Osmo_StopPump();
                        }
                        break;
                    }
                }
            }    
        }
        /* if reserve water is empty */
        else  
        {
            send_message(OSMOLATOR_MODULE_ID, alert, "reserve d'eau vide");
            if (OUTPUT_GetOutputValue(OSMOLATOR_PUMP) == TRUE)
            {
                send_message(OSMOLATOR_MODULE_ID, normal, "pump stop");
                Osmo_StopPump();
            }
        }
        
        wait(Osmo_TASK_DELAY);
    } /* Never exit this loop */ 
}

/**    
  * @brief  Initialisation of modules.    
  * @param  none    
  * @retval None    
*/  
void Osmolator_init(void)
{
    
}

/**    
  * @brief  Check the water level.    
  * @param  none    
  * @retval Level of water (low,middle,high)
*/ 
Osmo_Water_level_t Osmo_CheckWaterLevel(void)
{
  Osmo_Water_level_t Level;
    
  if ((INPUT_GetInputValue(OSMOLATOR_SENSOR_LOW)  == FALSE) &&
      (INPUT_GetInputValue(OSMOLATOR_SENSOR_HIGH) == FALSE))
  {
    Level = OSMO_HIGH_LEVEL;
  }
  else if ((INPUT_GetInputValue(OSMOLATOR_SENSOR_LOW)  == FALSE) &&
           (INPUT_GetInputValue(OSMOLATOR_SENSOR_HIGH) == TRUE))
  {
    Level = OSMO_MIDDLE_LEVEL;
  }
  else if ((INPUT_GetInputValue(OSMOLATOR_SENSOR_LOW)  == TRUE) &&
           (INPUT_GetInputValue(OSMOLATOR_SENSOR_HIGH) == FALSE))
  {
    Level = OSMO_LOW_LEVEL;
  }
  else if ((INPUT_GetInputValue(OSMOLATOR_SENSOR_LOW)  == TRUE) &&
           (INPUT_GetInputValue(OSMOLATOR_SENSOR_HIGH) == TRUE))
  {
    Level = OSMO_LOW_LEVEL;
  }
  else
  {
    /* if default case occur, we considered that level is high, so pump is not started */
    Level = OSMO_HIGH_LEVEL;
  }
        
  return Level;
}


/**    
  * @brief  Get the water leval status.    
  * @param  none    
  * @retval None    
*/ 
Osmo_Water_level_t Osmo_GetOsmolatorStatus()
{
    return Osmo_CheckWaterLevel();
}

#endif /* _OSMOLATOR */